<?php
namespace app\api\controller;

use think\Db;
use think\Config;
use Common\Oss;
use think\Exception;
class Service extends Base {
    /**
     * @writer:zhouminjin
     * @DateTime:2018-01-15
     * @rewriter:xx
     * @rewriter_date:YY-MM-DD
     * @description: 首页接口
     * @return \think\response\Json
     */
    public function suggestion(){
        $post = request()->post();
        $data['user_id'] = $post['user_id'];
        $data['suggestion_content'] = $post['content'];
        $data['tel']     = $post['tel'];
        $image_data = [];
        $image_ids = '';
        $ids = [];
        if(!empty($post)) {
            $images = json_decode($post['images'],true);
            foreach ($images as $key => $image) {
                $image_data['name'] = '意见建议';
                $image_data['image_url'] = $image;
                $image_data['create_time'] = date('Y-m-d H:i:s');
                $id = Db::name('image')->insert($image_data);
                $ids[] = $id;
            }
            $image_ids = implode(',',$ids);
        }
        $data['image_ids'] = $image_ids;
        $re = Db::name('suggestion')->insert($data);

        if($re){
            $res['status'] = 200;
            $res['msg']    = '意见提交成功';
        }else{
            $res['status'] = 203;
            $res['msg']    = '提交失败';
        }
        return json($res);
    }

    /**
     * @writer:zhouminjin
     * @DateTime:2018-01-15
     * @rewriter:xx
     * @rewriter_date:YY-MM-DD
     * @description: 问题组
     * @return \think\response\Json
     */
    public function problemType(){
        $re = Db::name('problem_type')
            ->field('id,name,name_english')
            ->where('status',1)
            ->select();
        if(!empty($re)){
            $res['status'] = 200;
            $res['msg']    = '查询成功';
            $res['type']['list'] = $re;
            $res['type']['count'] = count($re);
        }else{
            $res['status'] = 202;
            $res['msg']    = '数据为空';
        }
        return json($res);
    }

    public function problem(){
        $post = request()->post();
        if(!isset($post['type_id']) || !isset($post['user_id'])){
            $res['status'] = 201;
            $res['msg']    = '参数错误';
            return json($res);
        }
//        $service = Db::name('user')->alias('u')
//            ->field('c.*')
//            ->join('district d','d.postcode = u.postcode')
//            ->join('customer_service_district cd','cd.district_id = d.id')
//            ->join('customer_service c','c.id = cd.customer_service_id')
//            ->where('u.id',$post['user_id'])
//            ->find();
        $services = Db::name('system_setting')->where('name','客服电话')->where('flag',1)->find();
        $netTel = Db::name('system_setting')->where('name','在线客服')->where('flag',1)->find();
        $levelTel = Db::name('system_setting')->where('name','平台电话')->where('flag',1)->find();
        $re = Db::name('problem')
            ->field('name,name_english,answer')
            ->where('type_id',$post['type_id'])
            ->where('status',1)
            ->select();
        if(!empty($re)){
            if(empty($services)){
                $service['name'] = '';
                $service['tel'] = '';
                $service['id'] = '';
            }else{
                $service['name'] = '';
                $service['tel'] = $services['value'];
                $service['id'] = '';
            }
            $res['status'] = 200;
            $res['msg']    = '查询成功';
            $res['type']['list'] = $re;
            $res['type']['count'] = count($re);
            $res['service'] = $service;
            $res['netTel'] = $netTel;
            $res['levelTel'] = $levelTel;
        }else{
            $res['status'] = 202;
            $res['msg']    = '数据为空';
        }
        return json($res);
    }

    public function uploadImage(){
        try {
            $client = new Oss();
            $re = $client->uploadFile('user', 'img');
            if ($re) {
                $res['status'] = 200;
                $res['msg'] = '上传成功';
                $res['path'] = $re;
            } else {
                $res['status'] = 203;
                $res['msg'] = '上传失败';
            }
        }catch (Exception $e){
            $res['status'] = 203;
            $res['msg'] = '上传失败';
        }
        return json($res);
    }
}